﻿using AutomatedTestDataManagement.Models;
using System.Data;
using System.Linq;
using System.Web.Mvc;

namespace AutomatedTestDataManagement.Controllers
{
    public class StepKindController : Controller
    {
        private ApplicationTestDataEntities db = new ApplicationTestDataEntities();

        //
        // GET: /StepKind/

        public ActionResult Index()
        {
            return View(db.StepKinds.ToList());
        }

        //
        // GET: /StepKind/Details/5

        public ActionResult Details(int id = 0)
        {
            StepKind stepkind = db.StepKinds.Find(id);
            if (stepkind == null)
            {
                return HttpNotFound();
            }
            return View(stepkind);
        }

        //
        // GET: /StepKind/Create

        public ActionResult Create()
        {
            ViewBag.DataTable = new SelectList(db.TableMasters.Where(tm => tm.TableKind == "DATA" || tm.TableKind == "NAVI")
                                        .Select(tm => new { DataTable = tm.TableName }), "DataTable", "DataTable");
            return View();
        }

        //
        // POST: /StepKind/Create

        [HttpPost]
        public ActionResult Create(StepKind stepkind)
        {
            if (ModelState.IsValid)
            {
                db.StepKinds.Add(stepkind);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(stepkind);
        }

        //
        // GET: /StepKind/Edit/5

        public ActionResult Edit(int id = 0)
        {
            StepKind stepkind = db.StepKinds.Find(id);
            if (stepkind == null)
            {
                return HttpNotFound();
            }
            ViewBag.DataTable = new SelectList(db.TableMasters.Where(tm => tm.TableKind == "DATA" || tm.TableKind == "NAVI")
                                        .Select(tm => new { DataTable = tm.TableName }), "DataTable", "DataTable", stepkind.DataTable);
            return View(stepkind);
        }

        //
        // POST: /StepKind/Edit/5

        [HttpPost]
        public ActionResult Edit(StepKind stepkind)
        {
            if (ModelState.IsValid)
            {
                db.Entry(stepkind).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(stepkind);
        }

        //
        // GET: /StepKind/Delete/5

        public ActionResult Delete(int id = 0)
        {
            StepKind stepkind = db.StepKinds.Find(id);
            if (stepkind == null)
            {
                return HttpNotFound();
            }
            if (stepkind.TestSteps.Count > 0)
            {
                ModelState.AddModelError("", "This step kind is used by one or more Test Steps. It cannot be deleted while it is being used");
            }
            return View(stepkind);
        }

        //
        // POST: /StepKind/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            StepKind stepkind = db.StepKinds.Find(id);
            db.StepKinds.Remove(stepkind);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}